package com.ellecity06.debugtools.base

import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow

/**
 * @author ellecity06
 * @time 2023/4/6 14:41
 * @des
 */
open class BaseDebugViewModel : ViewModel(), LifecycleObserver {
    protected val mViewEffect = MutableSharedFlow<ViewEffect>()
    val viewEffect = mViewEffect.asSharedFlow()
    open var pageIndex = 1
    open val pageSize = 20
    open fun refresh() {
    pageIndex = 1

        loadData()
    }

    open fun loadMore() {

        loadData()
    }

    open fun loadData() = Unit

    /**
     * 加载更多为空
     */
    suspend fun loadMoreEmpty(message: String? = null) {
        mViewEffect.emit(ViewEffect.LoadMoreEmpty(message = message))
    }

    /**
     * 加载更多失败
     */
    suspend fun loadMoreFail(e: Exception) {
        mViewEffect.emit(ViewEffect.LoadMoreListFail(e))
    }

    /**
     * 加载更多成功
     */
    suspend fun loadMoreSuccess(list: List<*>, endOfPaginationReached: Boolean) {
        mViewEffect.emit(ViewEffect.LoadMoreListSuccess(list = list, endOfPaginationReached = endOfPaginationReached))
    }

    /**
     *刷新数据为空
     */
    suspend fun refreshEmpty(message: String? = null) {
        mViewEffect.emit(ViewEffect.RefreshListEmpty(message = message))
    }

    /**
     * 刷新失败了
     */
    suspend fun refreshFail(message: String, id: Int? = null) {
        mViewEffect.emit(ViewEffect.RefreshFail(message = message, id = id))
    }

    /**
     * 刷新成功
     */
    suspend fun refreshSuccess(list: List<*>, endOfPaginationReached: Boolean) {
        mViewEffect.emit(ViewEffect.RefreshListSuccess(list = list, endOfPaginationReached = endOfPaginationReached))
    }

    suspend fun showToast(message: String) {
        mViewEffect.emit(ViewEffect.ShowToast(message))
    }

    suspend fun showLoading(scope: CoroutineScope? = null) {
        mViewEffect.emit(ViewEffect.ShowLoading("加载中...", scope = scope))
    }

    suspend fun dismissLoading() {
        mViewEffect.emit(ViewEffect.DismissLoadingDialog)
    }

    suspend fun tokenExpired(message: String) {
        mViewEffect.emit(ViewEffect.AuthorizationExpired(message = message))
    }


}